#include "cbetsalarm.h"
#include "ui_cbetsalarm.h"
#include "csetalarmdialog.h"
#include "cplayalarm.h"
#include <QDebug>
#include <QFile>
#include <QTime>
#include <QDir>
#include <QTimer>

CBetsAlarm::CBetsAlarm(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CBetsAlarm)
{
    //
    //  UI
    //
    ui->setupUi(this);
    ui->loadFromFlashScoreButton->setDisabled( true );
    ui->editAlarmFileButton->setDisabled( true );


    m_pSetAlarm = new CSetAlarmDialog( this );


    //
    //  Bets Model
    //
    this->m_betModel.setColumnCount( 3 );

    QStringList headerList;
    headerList << "Time" << "FootbalTeams" << "Status";
    this->m_betModel.setHorizontalHeaderLabels( headerList );
    ui->tableView->setModel( &m_betModel );
    ui->tableView->horizontalHeader()->setResizeMode( QHeaderView::ResizeToContents );

    //
    //  PushButtons signals
    //
    bool bOk = connect( ui->setAlarm, SIGNAL( clicked() ),
                        this, SLOT( onSetAlarmButton() ) );
    Q_ASSERT( bOk );

    bOk = connect( ui->loadAlarmFileButton, SIGNAL( clicked() ),
                   this, SLOT( onLoadButton() ) );
    Q_ASSERT( bOk );    
}

CBetsAlarm::~CBetsAlarm()
{
    delete ui;
    delete m_pSetAlarm;
    foreach( CPlayAlarm *c, this->m_myAlarms )
    {
        delete c;
    }


}


void CBetsAlarm::onSetAlarmButton()
{
    m_pSetAlarm->show();
}

void CBetsAlarm::onLoadButton()
{
    //
    //  on load clear the old content
    //
    this->m_betModel.removeRows( 0, m_betModel.rowCount() - 1 );
    // ToDo: delete the alarms

    foreach( CPlayAlarm *c, this->m_myAlarms )
    {
        delete c;
    }
    this->m_myAlarms.clear();


    QString path = QDir::currentPath () + "/bets.txt";
    QFile file( path );
    if ( !file.open(QIODevice::ReadOnly|QIODevice::Text) )
    {
        qDebug()  << "blabla";
    }

    QTextStream in(&file);
    QString line = in.readLine();

    while (!line.isNull())
    {
        QStringList words = line.split( " " );
        QString timeString = words.at(0);
        QTime matchTime = QTime::fromString(timeString.remove( 5, timeString.length() - 5 ), "hh:mm" );

        if( ( !matchTime.isNull() ) && ( matchTime.addSecs( ( 90 + 15 + 5 ) * 60 ) >= QTime::currentTime() ) )
        {
             //
             //  Add  data to model
             //

             QString rest;
             for( int i = 1; i < words.length(); i++ )
                 rest = rest + words.at( i );
             addRowToModel( timeString,  rest );

             //
             //  Add alarms
             //
             int startAt = matchTime.secsTo( QTime::currentTime() );
             if (startAt <= 0)
             {
                  CPlayAlarm *alarm = new CPlayAlarm( timeString, rest, (-1) * startAt );
                  this->m_myAlarms.append( alarm );
             }

             if( ( startAt > 0 ) && startAt < ( 90 + 15 + 5 ) * 60 )
             {
                 CPlayAlarm *alarm = new CPlayAlarm( timeString, rest, 10 - ( qrand( ) % 10  ) );
                 this->m_myAlarms.append( alarm );
             }

        }
        line = in.readLine();
    }

    this->m_betModel.sort( 0 );
}

void CBetsAlarm::addRowToModel( const QString &time, const QString &info )
{
    m_betModel.setRowCount( m_betModel.rowCount() + 1 );

    QModelIndex index;
    index = m_betModel.index( m_betModel.rowCount() - 1, 0, QModelIndex());
    m_betModel.setData(index, time );

    qDebug() << index.row() << index.column();

    index = m_betModel.index( m_betModel.rowCount() - 1, 1, QModelIndex());
    m_betModel.setData(index, info );

    qDebug() << index.row() << index.column();

    index = m_betModel.index( m_betModel.rowCount() - 1, 2, QModelIndex());
    m_betModel.setData(index, "not implemented" );

}
